<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="../include/head.jsp" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%> 
<!DOCTYPE HTML>
<html>
<head>

<title>用户管理-用户信息</title>
<style type="text/css">

		html,
body {
  height:100%;
  margin:0;
  padding:0;
  font:small/1.5 "宋体", serif; 
  overflow-y: hidden;
}


</style>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/lib/easyui/1.5.3/themes/metro/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/lib/easyui/1.5.3/themes/icon.css?v=20171205">

<script type="text/javascript" src="${pageContext.request.contextPath}/lib/jquery/1.9.1/jquery.min.js"></script>  
<script type="text/javascript" src="${pageContext.request.contextPath}/lib/easyui/1.5.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lib/easyui/1.5.3/locale/easyui-lang-zh_CN.js"></script> 
<script type="text/javascript" src="${pageContext.request.contextPath}/lib/common/public.js?v=20180416"></script> 

</head>
<body>
 
<!-- left and right -->

 <div class="easyui-layout" style="width:100%;height:100%;">
       
        <div data-options="region:'west',split:false,title:'组织/机构',hideCollapsedContent:false" title="组织/机构" style="width:250px;">
        	 <table id="depts" class="easyui-treegrid"
                    data-options="url:'./findDepts',method:'get',border:false,singleSelect:true,fit:true,fitColumns:true,idField:'did',onClickRow:function(row){
            			 var did = row.did;
            			$('#users').datagrid('load' , {dept_id:did});
            			 
            		}">
      
                <thead>
                    <tr>
                        <th data-options="field:'department_name'" width="80">机构名称</th>
                        <th data-options="field:'crtime',formatter:function(v){
                        	var d = new Date(v);
                        	return dateFtt('yyyy-MM-dd hh:mm:ss',d); 
                        }" width="100">创建时间</th> 
                    </tr>
                </thead>
            </table>
         
        </div>
        <div data-options="region:'center',title:'用户信息',tools:'#user_toolbar'">
        	<shiro:hasPermission name="/ma/findUsers">
        		
        	<table id="users" class="easyui-datagrid"
                    data-options="url:'./findUsers',method:'get',checkbox: true,border:false,cascadeCheck:true,singleSelect:true,fit:true,fitColumns:true,idField:'id'">
                <thead>
                    <tr>
                    	<th data-options="field:'user_id'" width="80">登录ID</th>
                     	<th data-options="field:'user_name',align:'left'" width="130">用户名</th>
                        <th data-options="field:'create_time',align:'left',formatter:function(v){
                        	var d = new Date(v);
                        	return dateFtt('yyyy-MM-dd hh:mm:ss',d); 
                        }" width="130">创建时间</th>
                        <th data-options="field:'dept_name',align:'left'" width="130">直属部门</th>
                        <th data-options="field:'role_name',align:'left'" width="130">拥有角色</th>
                    </tr>
                </thead>
            </table>
            
            <shiro:hasPermission name="/ma/resetPass">
            	<div id="mm" class="easyui-menu" style="width:80px;">
        			<div onclick="resetPass()" data-options="iconCls:'icon-reload'">重置密码</div> 
    			</div>
    			
    			<script type="text/javascript">
    			 $('#users').datagrid({ 
    		        onRowContextMenu:function(e,index,row){
    		       	 if (row){
    		       		 e.preventDefault();
    		       		 $(this).datagrid('selectRow', index);
    		       		 $('#mm').menu('show',{
    		       			 left: e.pageX,
    		       			 top: e.pageY
    		       		 });                
    		       	 } 
    		       	
    		        }
    				 
    			 });
    			 
    			 function resetPass() {
    				var u = $('#users').datagrid('getSelected');
    				if(!u || !u.user_name || !u.user_id) return ;
 	 		 		 
 	 		 		$.messager.prompt('用户密码重置', '请输入用户:[' + u.user_name + ']的新密码:'  , function(r){
			            if (r){
			            	 $.ajax({  
						          url: './resetPass'   ,  
						          type: 'POST',  
						          data: {uid : u.id , pass : r},   
						          success: function (ret) {  
						        	 
						        	  if(ret.error) { 
						        		  $.messager.alert('失败',"重置失败:" + result.error,'error');
										 
										} else if(ret.success){ 
											$.messager.show({
								                title:'成功',
								                msg:'重置用户密码成功.',
								                timeout:3000,
								                showType:'slide'
								            });
											 
											
										}
						        	  $.messager.progress('close');
						          },  
						          error: function (returndata) {  
						             // alert('fail:' + returndata);  
						              console.log(JSON.stringify(returndata));
						              $.messager.progress('close');
						          } 
						     });   
			            }
 	 		 		});
			         
 	 		 			 
    			 }
    			
    			</script>
            	
            
            </shiro:hasPermission>
            
            <shiro:hasPermission name="/ma/addUser or /ma/updateUser">
            	<div id="user_ma"   class="easyui-dialog" title="用户信息" data-options="iconCls:'icon-add',closed:true,resizable:true,modal:true" style="width:400px;height:360px;padding:10px;display: none;">
	 		 <form id="ff" method="post"> 
	 		 	<input type="hidden" id="dept_id" name="dept_id">
	 		 	<input type="hidden" id="roles" name="roles">
	 		 	<input type="hidden" id="id" name="id">
	 		 <!-- 	<input type="hidden" id="dept_name" name="dept_name">
	 		 	<input type="hidden" id="role_name" name="role_name"> -->
	 		 	 <div style="margin-bottom:10px">
	 		 	 
              	     <input id="user_dept" name="user_dept" class="easyui-combotreegrid" style="width:95%" data-options="label:'对应部门' ,
        				idField:'did', 
        				treeField:'department_name',
        				url: './findDepts',
        				 columns:[[
                        {field:'department_name',title:'部门名称',width:200} 
                        
                    ]] "> 
            	</div> 
           	 	<div style="margin-bottom:10px">
                	<input class="easyui-textbox" name="user_id" id="user_id"  style="width:95%" data-options="label:'登录ID:',labelPosition:'left',required:true"> 
            	</div>
            	<div style="margin-bottom:10px">
            	    <input class="easyui-textbox" name="user_name" id="user_name" style="width:95%" data-options="label:'用户名:',labelPosition:'left',required:true"> 
            	</div>
            	<div style="margin-bottom:10px" id="pass_div">
               	 <input class="easyui-textbox" name="user_pass" id="user_pass" style="width:95%" data-options="label:'初始密码:',labelPosition:'left',required:true">
            	</div> 
            	<div style="margin-bottom:10px">
              	  <input class="easyui-textbox" name="description"  id="description" style="width:95%;height:60px" data-options="label:'备注信息:',labelPosition:'left',multiline:true">
            	</div> 
            	<div style="margin-bottom:10px">
              	 <input id="user_roles" name="user_roles" class="easyui-combobox" style="width:95%" data-options="label:'对应角色' ,
        				valueField: 'role_id',
        				textField: 'role_name',
        				url: './findRoles',
         				multiple:true "> 
            	</div> 
            	
        	</form>
        	<div style="text-align:center;padding:5px 0">
            	<a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:$('#ff').form('submit');" style="width:80px">保存</a>
            	<a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:$('#user_ma').dialog('close');" style="width:80px">关闭</a>
        	</div>
	 	</div>
	 	
	 		<script type="text/javascript">
	 			function formChange(target) {
	 				//判断当前选择的机构 
					if ($(target).attr('id') == 'user_dept') {
							//部门选择
							var dept_id = $(target).combotreegrid('getValue');
							if (dept_id) {
								$('#dept_id').val(dept_id);
								/* var dept_name = $(target).combotreegrid('getText');
								$('#dept_name').val(dept_name); */
							} else {
								$('#dept_id').val(0);
								/* $('#dept_name').val(''); */
							}
						} else if ($(target).attr('id') == 'user_roles') {
							//角色
							var vs = $('#user_roles').combobox('getValues');
							$('#roles').val(vs.join(','));
							/* 
							var role_names = $('#user_roles').combobox('getText');
							if(role_names)
								$('#role_name').val(role_names); */
						}
					}
				</script>
	 	
            </shiro:hasPermission>
             
            
           <div id="user_toolbar">
           	<shiro:hasPermission name="/ma/addUser"> 
        		<a href="#" class="easyui-linkbutton" iconCls="icon-add"  plain="true" onclick="addUser()">添加</a>
        		<script type="text/javascript">
        			function addUser() {
        				$('#ff').form('clear');
        				$('#pass_div').show();
        				$('#user_id').textbox('readonly',false);	// enable readonly mode
    	 		    	$('#user_ma').dialog('open');
    	 		    	
    	 		    	$('#ff').form({
    	 		    	    url: './addUser', 
    	 		    	   onChange : formChange,
    	 		    	    success:function(data){
    	 		    	       	var ret =JSON.parse(data) ;
    	 		    	        //console.log(data.error + "  " + data.success);
    	 		    	        if(ret.error) {
    	 		    	        	 $.messager.alert('提示',ret.error,'warning');
    	 		    	        }else if(ret.success) {
    	 		    	        	$('#user_ma').dialog('close');
    	 		    	        	   
    	 		 	    	        $('#users').datagrid('appendRow',ret.data);
    	 		 	    	       $('#ff').form('clear');
    	 		    	        }
    	 		    	       
    	 		    	    }
    	 		    	});  
        				 
        			}
        		
        		</script>
        		
        	</shiro:hasPermission>
        	<shiro:hasPermission name="/ma/updateUser"> 
        		<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="updateUser()">更新</a>
        		<script type="text/javascript">
        			function updateUser() {
        				var u = $('#users').datagrid('getSelected');
    	 		 		if(u && u.id) {
    	 		 			
    	 		 			$('#ff').form('load' , u);
    	 		 			
    	 		 			$('#pass_div').hide();
    	 		 			$('#user_pass').textbox('setValue', '123'); //密码在这里不更新
    	 		 			$('#user_id').textbox('readonly',true);	// enable readonly mode
    	 		 			 
    	 		    		var roles = u.roles ;
    	 		    		if(u.dept_id)
    	 		    			$('#user_dept').combotreegrid('setValue' , u.dept_id);
    	 		    		if(roles)
    	 		    			$('#user_roles').combobox('setValue', roles.split(','));
    	 		    		
    	 		    		
    	 		    	} else {
    	 		    		return ;
    	 		    	}
    	 		    	
    	 		    	$('#user_ma').dialog('open');
    	 		    	
    	 		    	$('#ff').form({
    	 		    	    url: './updateUser', 
    	 		    	   onChange: formChange,
    	 		    	    success:function(data){
    	 		    	       	var ret =JSON.parse(data) ;
    	 		    	        //console.log(data.error + "  " + data.success);
    	 		    	        if(ret.error) {
    	 		    	        	 $.messager.alert('提示',ret.error,'warning');
    	 		    	        }else if(ret.success) {
    	 		    	        	$('#user_ma').dialog('close');
    	 		    	        	var uid = $('#id').val();
    	 		    	        	var index = $('#users').datagrid('getRowIndex' , uid  );
    	 		 	    	      
    	 		 	    	        $('#users').datagrid('updateRow',{index: index, row:ret.data });
    	 		 	    	        $('#ff').form('clear');
    	 		 	    	       
    	 		 	    	      $.messager.show({
    	 			       	  			title:'提示',
    	 			       	  			msg: '更新成功' ,
    	 			       	  			timeout:2000,
    	 			       	  			showType:'slide'
    	 			       	  		});
    	 		    	        }
    	 		    	       
    	 		    	    }
    	 		    	});
        				 
        			}
        		
        		</script>
        		
        	</shiro:hasPermission>
        	<shiro:hasPermission name="/ma/removeUser">
        		<a href="#" class="easyui-linkbutton" iconCls="icon-remove"  plain="true" onclick="removeUser()">删除</a>
        		<script type="text/javascript">
	 			function removeUser() {
	 		 		
	 		 		var select = $('#users').datagrid('getSelected');
	 		    	if(select && select.id && select.user_name) {
	 		    		
	 		    		$.messager.confirm('提示','您确定要删除:' + select.user_name + " 这个用户吗?",function(r){
	 		    		    if (r){
	 		    		    	 $.messager.progress({
	 		    		  		        title:'请稍等',
	 		    		  		        msg:'正在删除...'
	 		    		  		    });
	 		    		    		 $.ajax({  
	 		    		     	         url: './removeUser' ,  
	 		    		     	         type: 'POST',  
	 		    		     	         data: {uid : select.id},    
	 		    		     	         success: function (ret) {   
	 		    		     	       	  	if(ret && ret.error) { 
	 		    		     	       	  	 	$.messager.alert('提示',ret.error,'warning');
	 		    		     				} else if(ret && ret.success){ 
	 		    		     					//数据
	 		    		     					$.messager.show({
	 		    		     	       	  			title:'提示',
	 		    		     	       	  			msg: '删除成功' ,
	 		    		     	       	  			timeout:3000,
	 		    		     	       	  			showType:'slide'
	 		    		     	       	  		});
	 		    		     					var index = $('#users').datagrid('getRowIndex',select.id);
	 		    		     					//删除该节点
	 		    		     					if(index >= 0)
	 		    		     						$('#users').datagrid('deleteRow',index);
	 		    		     					
	 		    		     				}
	 		    		     	       	  	$.messager.progress('close');
	 		    		     	         },  
	 		    		     	         error: function (returndata) {  
	 		    		     	            // alert('fail:' + returndata);   
	 		    		     	             $.messager.progress('close');
	 		    		     	         } 
	 		    		     	    });   
	 		    		    }
	 		    		});
	 		    		 
	 		    	}
	 		 		
	 		 	}
	 	  
	 			</script>
        	
        	</shiro:hasPermission>
           </div>
             
        	</shiro:hasPermission>
           
        </div>
    </div>


</body>
</html>